﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using DevComponents.DotNetBar;
using PlanningGIS.Util;
using System.Drawing;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;
using System.Windows.Forms;

namespace PlanningGIS.Engine.Controls
{
    /// <summary>
    /// 绘制Element对象
    /// </summary>
    public partial class CtlDrawElements : ButtonX
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CtlDrawElements()
        {
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            InitializeComponent();
            this.ColorTable = eButtonColor.Flat;
            this.BackColor = Color.Transparent;
            this.Style = eDotNetBarStyle.StyleManagerControlled;
            AddButton();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="container">控件容器</param>
        public CtlDrawElements(IContainer container)
        {
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            container.Add(this);
            InitializeComponent();
            AddButton();
        }

        /// <summary>
        /// 地图管理对象，MapControl.Object
        /// </summary>
        public object Hook { get; set; }

        /// <summary>
        /// 获取或设置当前选择的绘制工具对象
        /// </summary>
        public ESRI.ArcGIS.SystemUI.ICommand CurrentCommand { get; set; }

        /// <summary>
        /// 添加绘制工具按钮
        /// </summary>
        private void AddButton()
        {
            this.Tooltip = "绘制图形";
            List<ValueName> btns = new List<ValueName>()
            {
                new ValueName(){Value="ControlsNewRectangleTool",Name="绘制矩形"},
                new ValueName(){Value="ControlsNewPolygonTool",Name="绘制多边形"},
                new ValueName(){Value="ControlsNewCircleTool",Name="绘制圆"},
                new ValueName(){Value="ControlsNewEllipseTool",Name="绘制椭圆"},
                new ValueName(){Value="ControlsNewLineTool",Name="绘制线"},
                new ValueName(){Value="ControlsNewCurveTool",Name="曲线"},
                new ValueName(){Value="ControlsNewFreeHandTool",Name="手绘线"},
                new ValueName(){Value="ControlsNewMarkerTool",Name="绘制点"}
            };
            foreach (ValueName v in btns)
            {
                ButtonItem btn = new ButtonItem(v.Value, v.Name);
                btn.Tag = Activator.CreateInstance(Type.GetTypeFromProgID("esriControls." + v.Value));
                string imagename = v.Value.Replace("Controls", "").Replace("Tool", "");
                Bitmap bmp = (Bitmap)(Properties.Resources.ResourceManager.GetObject(imagename));
                bmp.MakeTransparent();
                btn.Image = bmp;
                btn.Click += new EventHandler(btn_Click);
                this.SubItems.Add(btn);
            }
            this.Image = ((ButtonItem)this.SubItems[0]).Image;
            this.Tag = ((ButtonItem)this.SubItems[0]).Tag;
            this.Click += new EventHandler(CtlDrawElements_Click);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CtlDrawElements_Click(object sender, EventArgs e)
        {
            CurrentCommand = this.Tag as ESRI.ArcGIS.SystemUI.ICommand;
            SelectedChanged();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void btn_Click(object sender, EventArgs e)
        {
            ButtonItem btn = (ButtonItem)sender;
            this.Image = btn.Image;
            this.Tag = btn.Tag;
            if (btn.Tag == null) return;
            CurrentCommand = btn.Tag as ESRI.ArcGIS.SystemUI.ICommand;
            SelectedChanged();
            
        }
        /// <summary>
        /// 
        /// </summary>
        protected virtual void SelectedChanged()
        {
            if (Hook != null && CurrentCommand != null)
            {
                CurrentCommand.OnCreate(Hook);
                CurrentCommand.OnClick();
            }
        }
    }
}
